define(["angular","config","store","angular-route","angular-ui-router","angular-bootstrap","angular-local-zh-cn","jquery.cookie"], function(angular,config,store){
    var JSESSIONID = $.cookie("JSESSIONID");
    if(JSESSIONID){
        window.JSESSIONID = JSESSIONID;
    }else{
        window.location.href = config.basePath + "login.html";
    }
    var app = angular.module("app",["ngRoute","ui.bootstrap","ui.router","uiLocalConfig"]);

    app.controller("bodyController", function($scope){
        $scope.BOSS_USER_INFO = $.parseJSON($.cookie("BOSS_USER_INFO"));
        $scope.loginout = function(){
            $.cookie("BOSS_USER_INFO", null, {expires:-1, path:"/"});
            $.cookie("JSESSIONID", null, {expires:-1, path:"/"});
            store.remove('permissionExpire');
            store.remove('permissionStore');
        }
    });

    //监控全局ajax 添加动态提示
    app.directive('loading', function ($http,$interval) {
        return {
            restrict: 'E',
            replace:true,
            template: '<div class="loading"><span ng-bind="timeSecond"></span><b class="fa fa-spinner fa-spin fa-3x"></b></div>',
            link: function (scope, element, attr) {
                scope.isLoading = function () {
                    return $http.pendingRequests.length>0 || scope.jQueryAjaxLoading;
                };
                //集成jQuery.ajax提示
                $(document).ajaxStart(function(){
                    scope.jQueryAjaxLoading = true;
                });
                $(document).ajaxStop(function(){
                    scope.jQueryAjaxLoading = false;
                });
                var timeout;
                scope.timeSecond=0;
                scope.$watch(scope.isLoading, function (v){
                    if(v){
                        element.show();
                        //设置ajax计时器
                        timeout = $interval(function(){
                            scope.timeSecond++;
                            if(scope.timeSecond>99){
                                element.hide();
                                angular.forEach($http.pendingRequests, function(request) {
                                    if (request.cancel && request.timeout) {
                                        request.cancel.resolve();
                                    }
                                });
                                alert("请求超时，请联系管理员！");
                                $interval.cancel(timeout);
                                scope.timeSecond = 0;
                            }
                        },1000);
                    }else{
                        element.hide();
                        $interval.cancel(timeout);
                        scope.timeSecond = 0;
                    }
                });
            }
        }
    });
    //添加拦截器，防止html缓存问题
    app.factory('httpInterceptor', [ '$q', '$injector', function($q, $injector) {
        var httpInterceptor = {
            'request' : function(request) {
                var url = request.url;
                if(url&&url.indexOf(".html")>0&&(url.indexOf("js/views")>-1||url.indexOf("js/modal")>-1)){
                    request.url += "?"+config.urlArgs;
                }
                return request;
            }
        };
        return httpInterceptor;
    }]);
    app.config(['$httpProvider', function($httpProvider) {
        $httpProvider.interceptors.push('httpInterceptor');
    }]);


    //临时包含页面用
    app.directive('dcInclude', function ($http,$interval) {
        return {
            restrict: 'EA',
            transclude: true,
            template: function(tElement,tAttrs){
                return '<div ng-include="\'' + config.basePath + tAttrs.dcInclude + '\'"></div>';
            },
            scope: false
        }
    });

    return app;
});